package com.hzx.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class Test {

    private static String a = "aaaa" ;

    @RequestMapping("/test")
    public String fileUpload(MultipartFile fileImage) throws IOException {

        System.out.println(fileImage.getOriginalFilename());

        final String imagePathRoot = "C:\\Users\\49975\\Desktop\\数据库\\DBPJ\\spring02\\src\\main\\resources\\UserHead";
        File file = new File(imagePathRoot);

        if (!file.exists()) {
            file.mkdirs();
        }

        String fileName = fileImage.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String imageFilePath = imagePathRoot + uuid + fileType;
        fileImage.transferTo(new File(imageFilePath));
        return "upload ok";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("img") MultipartFile file, HttpServletRequest request) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "C:\\Users\\49975\\Desktop\\数据库\\DBPJ\\spring02\\src\\main\\resources\\UserHead\\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
        }
        return "上传失败！";
    }





}

